YZYZ.Admin 					= {};
YZYZ.Admin.Scenic 			= {};
YZYZ.Admin.Scenic.Picture 	= {};
YZYZ.Admin.Scenic.ImagePanel= null;
YZYZ.Admin.Scenic.Picture.formPanel = null;
YZYZ.Admin.Scenic.Picture.uploadPanel = null;
YZYZ.Admin.Scenic.Picture.container = null;
YZYZ.Admin.Scenic.Picture.store = null;

YZYZ.Admin.Scenic.Picture.update = function(){
	var name = Ext.getCmp("name").getValue();
	var description = Ext.getCmp("description").getValue();
	if(Ext.isEmpty(name)){
		Ext.getCmp("name").markInvalid();
		YZYZ.Curtain.alert("图片名字不能为空");
		return;
	}
	if(description.length > 100) {
		Ext.getCmp("description").markInvalid();
		YZYZ.Curtain.alert("图片描述文字不能超过100个汉字");
		return;
	}
	
	var records = Ext.getCmp("dvImage").getSelectedRecords();
	if(!records){
		return;
	}
	var id = records[0].get("id");
	Ext.Ajax.request( {
		url		: Ext.get("updateUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params  : {
			name : name,
			description: description,
			pictureId: id
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("更新成功");
				records[0].set("name", name);
				records[0].set("description", description);
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序出现错误或网络故障");
		}
	});
};
YZYZ.Admin.Scenic.Picture.view = function(id){
	Ext.get("imgView").dom.src= document.getElementById("editUrl").value + id;
	var records = Ext.getCmp("dvImage").getSelectedRecords();
	Ext.getCmp("name").setValue(records[0].get("name"));
	Ext.getCmp("description").setValue(records[0].get("description"));
};
YZYZ.Admin.Scenic.Picture.checkImage = function(){
	var jpg = /^.+(\.jpg|\.jpeg)$/;
	if(!jpg.test(document.getElementById("iptImage").value)) {
		YZYZ.Curtain.alert("请上传格式为jpg的图片");
		return;
	}
	if(document.getElementById("iptImage").value.length <= 0){
		YZYZ.Curtain.alert("请上传格式为jpg的图片");
		return;
	}
	document.getElementById("frUpload").submit();
};
YZYZ.Admin.Scenic.Picture.remove = function(){
	var records = Ext.getCmp("dvImage").getSelectedRecords();
	if(!records){
		return;
	} else {
	var id = records[0].get("id");
	Ext.Ajax.request( {
		url		: Ext.get("deleteUrl").dom.value + records[0].get("id"),
		timeout	: 30000,
		method	: 'POST',
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("删除成功");
				Ext.getCmp("dvImage").getStore().remove(records[0]);
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序出现错误或网络故障");
		}
	});
		
	}
};

YZYZ.Admin.Scenic.Picture.init = function(){
	YZYZ.Admin.Scenic.Picture.uploadPanel = new Ext.Panel({
		width:450,
		height:100,
		contentEl:'divContainer',
		layout:'fit',
		region:'north'
	});
	YZYZ.Admin.Scenic.Picture.store = new Ext.data.JsonStore({
        url: Ext.get("loadUrl").dom.value + "&scenicId=" + Ext.get("scenicId").dom.value,
        root: 'images',
        fields: ['name', 'url', 'id','description']
    });
    YZYZ.Admin.Scenic.Picture.store.load();
    var tpl = new Ext.XTemplate(
		'<tpl for=".">',
            '<div class="thumb-wrap" id="{name}">',
		    '<div class="thumb"><img src="{url}" title="{name}" ondblclick="YZYZ.Admin.Scenic.Picture.view({id})"></div>',
		    '<span class="x-editable">{shortName}</span></div>',
        '</tpl>',
        '<div class="x-clear"></div>'
	);
	YZYZ.Admin.Scenic.ImagePanel = new Ext.Panel({
        id:'images-view',
        frame:true,
        width:400,
        collapsible:false,
        autoScroll:true,
        layout:'fit',
        region:'center',
        title:'酒店图片',
        items: new Ext.DataView({
            store: YZYZ.Admin.Scenic.Picture.store,
            id:'dvImage',
            tpl: tpl,
            autoHeight:true,
            multiSelect: true,
            overClass:'x-view-over',
            itemSelector:'div.thumb-wrap',
            emptyText: '您还没有添加图片',
            prepareData: function(data){
                data.shortName = Ext.util.Format.ellipsis(data.name, 15);
                return data;
            }
        })
    });
	YZYZ.Admin.Scenic.Picture.container = new Ext.Panel({
		layout:'border',
		region:'center',
		width:450,
		height:600,
		items:[YZYZ.Admin.Scenic.Picture.uploadPanel,YZYZ.Admin.Scenic.ImagePanel]
	});

	new Ext.Viewport({
		layout:'border',
		items:[{
			region:'center',
			items:[YZYZ.Admin.Scenic.Picture.container],
			width:450,
			autoScroll:true,
			layout:'fit'
		},{
			region:'east',
			contentEl		:'divViewZone',
			width			: 400,
			split 			: true,
			collapsible 	: true,
			title 			: '图片详细信息',
			autoScroll			: true,
			layout:'fit'
		}]
	});
	YZYZ.Admin.Scenic.Picture.formPanel = new Ext.form.FormPanel({
		width:380,
		labelWidth:50,
		applyTo:'divInfo',
		labelAlign:'right',
		buttonAlign:'right',
		buttons:[{
			xtype:'button',
			text:'提交',
			handler:YZYZ.Admin.Scenic.Picture.update
		},{
			xtype:'button',
			text:'删除',
			handler:YZYZ.Admin.Scenic.Picture.remove
		}],
		items:[{
			xtype:'textfield',
			width:300,
			id:'name',
			fieldLabel:'名称'
		},{
			xtype:'textarea',
			width:300,
			fieldLabel:'描述',
			id:'description'
		}]
	});
};
Ext.onReady(YZYZ.Admin.Scenic.Picture.init,document);